﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SIOsystem.Control;

namespace SIOsystem
{
    public partial class OutForm : Form
    {
        public string uname = "";
        public long id = -1;
        public string rid = "";
        private BindingList<OrderListClass> orderListDateSet = new BindingList<OrderListClass>();
        public OutForm()
        {
            InitializeComponent();
        }

        private void MainClassList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (MainClassList.Items.Count > 0)
            {
                long MID = 0;
                if (MainClassList.SelectedValue != null)
                {
                    long.TryParse(MainClassList.SelectedValue.ToString(), out MID);
                }
                if (MID > 0)
                {
                    DictionaryClass.fillProductClass(ref ProductList, MID);
                }
                else
                {
                    ProductList.DataSource = null;
                    ProductList.Items.Clear();
                }
            }
        }

        private void ProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProductList.Items.Count > 0)
            {
                long PID = 0;
                if (ProductList.SelectedValue != null)
                {
                    long.TryParse(ProductList.SelectedValue.ToString(), out PID);
                }
                if (PID > 0)
                {
                    DictionaryClass.fillModelClass(ref ModelList, PID);
                    ProductLabel.Text = ProductList.Text;
                }
                else
                {
                    ProductLabel.Text = "";
                    ModelList.DataSource = null;
                    ModelList.Items.Clear();
                }
            }
        }

        private void ModelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ModelList.Items.Count > 0)
            {
                long TID = 0;
                if (ModelList.SelectedValue != null)
                {
                    long.TryParse(ModelList.SelectedValue.ToString(), out TID);
                }
                if (TID > 0)
                {
                    JoinOrder.Enabled = true;
                    double[] storageAndPrice = OrderClass.getStorage(TID);
                    int listedAmt = 0;
                    foreach (OrderListClass ol in orderListDateSet)
                    {
                        if (ol.Tid==TID)
                        {
                            listedAmt += ol.Amt;
                        }
                    }
                    StorageLabel.Text = (storageAndPrice[0]-listedAmt).ToString("#0");
                }
                else
                {
                    StorageLabel.Text = "0";
                    JoinOrder.Enabled = false;
                }
            }
        }

        private void JoinOrder_Click(object sender, EventArgs e)
        {
            long MID = 0, PID = 0, TID = 0;
            int needAmt = Convert.ToInt32(InAmtPick.Value);
            if (needAmt==0)
            {
                MessageBox.Show("请填写提货数量");
                InAmtPick.Focus();
            }
            if (needAmt > int.Parse(StorageLabel.Text))
            {
                MessageBox.Show("库存不足，不能提货");
                return;
            }
            if (MainClassList.SelectedValue != null)
            {
                long.TryParse(MainClassList.SelectedValue.ToString(), out MID);
            }
            if (ProductList.SelectedValue != null)
            {
                long.TryParse(ProductList.SelectedValue.ToString(), out PID);
            }
            if (ModelList.SelectedValue != null)
            {
                long.TryParse(ModelList.SelectedValue.ToString(), out TID);
            }
            if (MID > 0 && PID > 0 && TID > 0)
            {
                try
                {
                    int outAmt = 0;
                    int listedAmt = 0;
                    long iSID = 0;
                    foreach (DataRow dr in OrderClass.getStorageList(TID).Rows)
                    {
                        if (needAmt<=0)
                        {
                            break;
                        }
                        iSID = Convert.ToInt64(dr["ISID"].ToString());
                        listedAmt = 0;
                        foreach (OrderListClass ol in orderListDateSet)
                        {
                            if (ol.Tid == TID && iSID == ol.ISID)
                            {
                                listedAmt += ol.Amt;
                            }
                        }
                        outAmt = Convert.ToInt32(dr["库存"].ToString()) - listedAmt;
                        if (outAmt<=0)
                        {
                            continue;
                        }
                        if (needAmt < outAmt)
                        {
                            outAmt = needAmt;
                        }
                        orderListDateSet.Add(
                            new OrderListClass(MID, PID, TID
                                , ProductLabel.Text + " " + ModelList.Text
                                , outAmt
                                , Convert.ToDouble(dr["价格"].ToString())
                                , Convert.ToInt64(dr["ISID"].ToString())
                                , remark.Text));
                        needAmt -= outAmt;
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("提货遇到错误："+ ex.Message);
                    return;
                }
                ReBinding();
                InAmtPick.Value = 0;
                ModelList.SelectedIndex = 0;
                remark.Text = "";
                ModelList.Focus();
            }
            else
            {
                MessageBox.Show("商品分类异常！");
            }
        }

        private void ReBinding()
        {
            double sum = 0;
            foreach (OrderListClass ol in orderListDateSet)
            {
                sum += ol.Amt * ol.Price;
            }
            MoneySum.Text = sum.ToString("#0.00#");
            OrderList.DataSource = new BindingList<OrderListClass>(orderListDateSet);
        }

        private void InsertOrder_Click(object sender, EventArgs e)
        {
            if (orderListDateSet.Count == 0)
            {
                MessageBox.Show("出库单为空！出库前先将商品信息添加进出库单");
                return;
            }
            if (DepartmentList.SelectedIndex == 0)
            {
                MessageBox.Show("请选择出库部门！");
                return;
            }
            if (OrderClass.insertOrder(orderListDateSet, OutputName.Text, Convert.ToInt64(DepartmentList.SelectedValue.ToString()), id))
            {
                MessageBox.Show("出库成功！");
                orderListDateSet.Clear();
                ReBinding();
                OutputName.Text = "";
                MainClassList.SelectedIndex = 0;
                DepartmentList.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("商品信息有误，出库失败，请检查出库单中数据格式");
            }
        }

        private void OutForm_Load(object sender, EventArgs e)
        {
            DictionaryClass.fillMainClass(ref MainClassList);
            DictionaryClass.fillDepartment(ref DepartmentList);
            orderListDateSet.Clear();
            ReBinding();
        }

        private void OrderList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            foreach (OrderListClass ol in orderListDateSet)
            {
                if (ol.ISID > orderListDateSet[e.RowIndex].ISID)
                {
                    MessageBox.Show("为了保证出库按先进先出原则。不能删除较早入库的商品!\n请先删除晚于本记录入库的商品。");
                    return;
                }
            }
            orderListDateSet.RemoveAt(e.RowIndex);
            ReBinding();
        }
    }
}
